package com.example.agriapp.model;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import androidx.annotation.DrawableRes;

import com.example.agriapp.ProductContract;
import com.example.agriapp.R;

import java.io.ByteArrayOutputStream;

public class TestDataUtils {
    public static void insertTestData(Context context) {
        new InsertTestDataTask(context).execute();
    }

    private static class InsertTestDataTask extends AsyncTask<Void, Void, Void> {
        private final Context context;

        InsertTestDataTask(Context context) {
            this.context = context.getApplicationContext();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            ContentResolver contentResolver = context.getContentResolver();

            // 清空现有数据
            contentResolver.delete(ProductContract.ProductEntry.CONTENT_URI, null, null);

            // 添加5条测试数据
            insertTestProduct(context, contentResolver, "新鲜苹果", 5.99, 100, "水果蔬菜", R.drawable.apple);
            insertTestProduct(context, contentResolver, "草莓", 3.49, 80, "水果蔬菜", R.drawable.strawberry);
            insertTestProduct(context, contentResolver, "优质大米", 12.99, 50, "米面粮油", R.drawable.rice);
            insertTestProduct(context, contentResolver, "散养鸡蛋", 8.99, 120, "米面粮油", R.drawable.eggs);
            insertTestProduct(context, contentResolver, "粉条", 8.99, 120, "米面粮油", R.drawable.sweet_potato_noodles);
            insertTestProduct(context, contentResolver, "蜂蜜", 6.50, 60, "米面粮油", R.drawable.honey);

            return null;
        }

        private void insertTestProduct(Context context, ContentResolver contentResolver,
                                       String title, double price, int quantity,
                                       String category, int imageResId) {
            ContentValues values = new ContentValues();
            values.put(ProductContract.ProductEntry.COLUMN_TITLE, title);
            values.put(ProductContract.ProductEntry.COLUMN_PRICE, price);
            values.put(ProductContract.ProductEntry.COLUMN_QUANTITY, quantity);
            values.put(ProductContract.ProductEntry.COLUMN_CATEGORY, category);

            // 将图片资源转换为字节数组
//            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imageResId);
//            ByteArrayOutputStream stream = new ByteArrayOutputStream();
//            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
//            byte[] imageBytes = stream.toByteArray();


            values.put(ProductContract.ProductEntry.COLUMN_IMAGE,Uri.parse("android.resource://" + context.getPackageName() + "/" + imageResId).toString() );

            contentResolver.insert(ProductContract.ProductEntry.CONTENT_URI, values);
        }
    }

    // 方法1：通过资源ID直接转换
    private String drawableToUri(Context context, int drawableId) {
        return Uri.parse("android.resource://" + context.getPackageName() + "/" + drawableId).toString();
    }


    // 可选：带图片的插入方法（确保图片资源正确后再使用）
    private static void insertProductWithImage(Context context, ContentResolver contentResolver,
                                               String title, double price, int quantity,
                                               String category, int imageResId) {
        ContentValues values = new ContentValues();
        values.put(ProductContract.ProductEntry.COLUMN_TITLE, title);
        values.put(ProductContract.ProductEntry.COLUMN_PRICE, price);
        values.put(ProductContract.ProductEntry.COLUMN_QUANTITY, quantity);
        values.put(ProductContract.ProductEntry.COLUMN_CATEGORY, category);

        try {
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imageResId);
            if (bitmap != null) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)) {
                    values.put(ProductContract.ProductEntry.COLUMN_IMAGE, stream.toByteArray());
                }
                bitmap.recycle();
            } else {
                Log.w("TAG", "无法解码图片资源: " + imageResId);
            }
        } catch (Exception e) {
            Log.e("TAG", "处理图片时出错", e);
        }

        try {
            contentResolver.insert(ProductContract.ProductEntry.CONTENT_URI, values);
        } catch (Exception e) {
            Log.e("TAG", "插入产品失败: " + title, e);
        }
    }

}